<script>

    /* 
    
    */





    //    B.prototype = new A();
    //    B.prototype.__proto__ = A.prototype;

    //    p = A.prototype 
    //    p.constructor = B
    //    B.prototype = p;


    function Person(name, age, gender, cars) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.cars = cars;
    }

    Person.prototype.say = function () {
        console.log(this.name + '在说话.....');
    }

    // 子元素 
    function Student(name, age, gender, cars, score) {
        // 借代函数 改变this的指向 
        Person.call(this, name, age, gender, cars)
        this.score = score;
    }
    // 如何继承父元素的原型
    Student.prototype = new Person();
    Student.prototype.constructor = Student;
    Student.prototype.exam = function () {
        console.log(this.name + '在考试.....');
    }

    // stu 继承了父元素
    const p = new Person("小红", 18, "女", ["奥迪", "宝马"]);
    const stu = new Student("小明", 18, "男", ["奔驰", "宝马"], 100);
    p.cars[0] = "保时捷"
    console.dir(p)
    console.dir(stu)


    // 寄生组合式继承
    function inheritPrototype (subType,superType){
        // 3.1.先创建一个对象，将父类原型作为对象
        var prototype = Object(superType.prototype);
        console.log(prototype);
        // 3.2.将创建的对象的构造函数指向子类  原指向父类
        prototype.constructor = subType;
        // 3.3.将父类原型的方法继承到子类的原型上
        subType.prototype = prototype;
    }
    // 1.创建父类 定义name colors  和在原型上定义一个sayName方法
    function Super(name){
        this.name = name;
        this.colors = ['red','blue']
    }
    Super.prototype.sayName = function(){
        console.log(this.name);
    }
    // 2.创建子类，子类继承父类的
    function Sub(name,age){
        // 将父类的属性继承过来，原型上的方法并没有继承
        Super.call(this,name);
        this.age = age;
    }
    // 3.继承父类原型上的方法，改变constructor的指向
    inheritPrototype(Sub,Super);
    // 4.为子类的原型上添加方法
    Sub.prototype.sayAge= function(){
        console.log(this.age);
    }

    var sub = new Sub("小明",18);
    // 为子类添加的原型方法，父类的原型上也存在
    var sup = new Super("笑话")
    console.log(sub);
    console.log(sup);
</script>